package com.adinnet.admin.order.service.impl;

import com.adinnet.admin.order.model.Order;
import com.adinnet.admin.order.mapper.OrderMapper;
import com.adinnet.admin.order.model.vo.OrderReq;
import com.adinnet.admin.order.model.vo.OrderResp;
import com.adinnet.admin.order.service.OrderService;
import com.adinnet.admin.peaseRecord.model.vo.PeaseRecordResp;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.common.biz.enums.RoleCode;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-16
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    /**
     * @param orderReq:
     * @param sysUserInfo: 超级管理员查所有
     *                     机构管理员查机构下校区
     *                     校区管理员查校区
     * @author zYshuai
     * @Date: 2020/12/16 13:39
     * @return: java.util.List<com.adinnet.admin.order.model.vo.OrderResp>
     */
    @Override
    public PageResult<OrderResp> orderList(OrderReq orderReq, SysUserInfo sysUserInfo) {
        if ((Long) orderReq.getPage() == null || (Long) orderReq.getLimit() == null) {
            orderReq.setPage(1);
            orderReq.setLimit(10);
        }
        Page page = new Page<OrderResp>(orderReq.getPage(), orderReq.getLimit());
        List<OrderResp> orderResps = Lists.newArrayList();
        if (StringUtils.isNotEmpty(orderReq.getSearchDate())) {
            orderReq.setStartDate(orderReq.getSearchDate().substring(0, 10) + " 00:00:00");
            orderReq.setEndDate(orderReq.getSearchDate().substring(13) + " 23:59:59");
        }else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            String now = format.format(new Date());
            calendar.setTime(new Date());
            calendar.add(Calendar.MONTH, -1);
            Date m = calendar.getTime();
            String mon = format.format(m);
            orderReq.setStartDate(mon);
            orderReq.setEndDate(now);
        }
        if (RoleCode.ADMIN.getCode().equals(sysUserInfo.getMaxRole())) {
            orderResps = orderMapper.orderList(page, orderReq.getSchoolId(), orderReq.getOrganizationId(), orderReq.getKeyWords(), orderReq.getStatus(), orderReq.getStartDate(), orderReq.getEndDate());
        }
        if (RoleCode.ORGANIZATION.getCode().equals(sysUserInfo.getMaxRole())) {
            orderReq.setOrganizationId(sysUserInfo.getOrganizationId());
            orderResps = orderMapper.orderList(page, orderReq.getSchoolId(), orderReq.getOrganizationId(), orderReq.getKeyWords(), orderReq.getStatus(), orderReq.getStartDate(), orderReq.getEndDate());
        }
        if (RoleCode.SCHOOL.getCode().equals(sysUserInfo.getMaxRole())) {
            orderReq.setSchoolId(sysUserInfo.getSchoolId());
            orderResps = orderMapper.orderList(page, orderReq.getSchoolId(), orderReq.getOrganizationId(), orderReq.getKeyWords(), orderReq.getStatus(), orderReq.getStartDate(), orderReq.getEndDate());
        }
//        if (RoleCode.CLASSS.getCode().equals(sysUserInfo.getMaxRole())) {
//            orderReq.setSchoolId(sysUserInfo.getSchoolId());
//            orderResps = orderMapper.orderList(orderReq.getSchoolId(), orderReq.getOrganizationId(), orderReq.getKeyWords(), orderReq.getStatus(), orderReq.getStartDate(), orderReq.getEndDate());
//        }
        return new PageResult<OrderResp>(orderResps, page.getTotal());
    }

    /**
     * @param orderId:
     * @Description: 确认收货
     * @author zYshuai
     * @Date: 2020/12/16 15:03
     * @return: com.adinnet.core.JsonResult
     */
    @Override
    public JsonResult updateStatus(Integer orderId) {
        Order order = orderMapper.selectById(orderId);
        if (null == order) {
            return JsonResult.error("无该订单");
        }
        order.setStatus(1);
        orderMapper.updateById(order);
        return JsonResult.ok("收货成功!");
    }
}
